package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AddGroup;
import com.ruoyi.system.domain.Member;
import com.ruoyi.system.service.AddGroupService;
import com.ruoyi.system.service.MemberService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： MemberController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/Member")
public class MemberController {

    @Resource
    private MemberService memberService;

    //查询
    @PostMapping("/get/Member")
    public AjaxResult getMember(@RequestBody Member member){
        //创建分页对象
        Page<Member> page = new Page<>();
        //设置页码
        page.setCurrent(member.getCurrent());
        //设置行数
        page.setSize(member.getSize());
        //执行查询
        memberService.page(page,new LambdaQueryWrapper<Member>()
                //群聊名称
                .like(StringUtils.isNotBlank(member.getMemberName()),Member::getMemberName,member.getMemberName())
                //逻辑删除
                .eq(Member::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Member::getCreateTime)
                //添加分页语句
                .last(" limit " + (member.getCurrent() - 1) * member.getSize() +"," + member.getSize())
        );
        page.setTotal(memberService.count(new LambdaQueryWrapper<Member>()
                //群聊名称
                .like(StringUtils.isNotBlank(member.getMemberName()),Member::getMemberName,member.getMemberName())

                //逻辑删除
                .eq(Member::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Member::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/Member/{id}")
    public AjaxResult deleteMember(@PathVariable(value = "id")Long id){
        //调用Service层删除
        memberService.update(null,new UpdateWrapper<Member>().lambda()
                //主键id
                .eq(Member::getId,id)
                //设置逻辑删除为 真
                .set(Member::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }
    
    //增加
    @PostMapping("/insert/Member")
    public AjaxResult insertMember(@RequestBody Member member){
        //设置默认值
        member.setCreateBy(SecurityUtils.getUsername());
        member.setCreateTime(new Date());
        member.setUpdateBy(SecurityUtils.getUsername());
        member.setUpdateTime(new Date());
        member.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        memberService.save(member);
        //返回结果
        return AjaxResult.success("新增成功");
    }

    //修改
    @PostMapping("/update/Member")
    public AjaxResult updateMember(@RequestBody Member member){
        //设置默认值
        member.setUpdateTime(new Date());
        member.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        memberService.updateById(member);
        //返回结果
        return AjaxResult.success("修改成功");
    }
}